package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.view.View
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.UserInfoManageUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.ProjectDetailActivityDelegate
import cc.vv.btpartyjob.partymanager.moudel.bean.ProjectDetailObj
import cc.vv.btpartyjob.partymanager.moudel.resp.ProjectDetailResponseObj
import cc.vv.btpartyjob.partymanager.server.PartyManagerServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 项目详情
 * Created by Chen.qi on 2018/10/30.
 */
class ProjectDetailActivity : BaseActivityMVP<ProjectDetailActivityDelegate>() {
    /**
     * 项目详情id
     */
    private var projectId: String? = null

    private var dataObj: ProjectDetailObj? = null

    override fun initData() {
        viewDelegate?.setTopBarTitle("项目详情")
        projectId = intent.getStringExtra("id")
        PartyManagerServer.projectDetail(projectId, true)
    }


    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //取消
            val intent = Intent(this, ProjectCancelActivity::class.java)
            intent.putExtra("projectDetailId", projectId)
            intent.putExtra("type", "3")
            startActivityForResult(intent, 100)
        }, R.id.tv_adp_cancelApply)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //延期申请
            val intent = Intent(this, ProjectPostponeApplyActivity::class.java)
            intent.putExtra("projectDetailId", projectId)
            intent.putExtra("jieDuanList", dataObj)
            startActivityForResult(intent, 100)
        }, R.id.tv_adp_yanQi)
    }

    override fun getDelegateClass(): Class<ProjectDetailActivityDelegate> {
        return ProjectDetailActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is ProjectDetailResponseObj -> {
                val data = obj.data
                dataObj = data
                viewDelegate?.setProjectName(data?.projectName)
                viewDelegate?.setProjectIntroduceContent(data?.projectIntroduction)
                viewDelegate?.setProjectType(data?.projectType)
//                viewDelegate?.setProjectWeekType(data?.projectCycle)
                viewDelegate?.setProjectPrincipalName(data?.principal)
                viewDelegate?.setProjectPrincipalPhone(data?.mobile)
                viewDelegate?.setProjectShenBaoTime(data?.declarationTime)
                viewDelegate?.setKeJian(data?.visibleRange)
                val segmentationList = data?.segmentationList

                viewDelegate?.setProjectProessJieDuan(data?.segmentationList, data?.sysState)

                //整体数据
                viewDelegate?.setZTContent("")
                viewDelegate?.setZTWarningTime(data?.warningTime)
                viewDelegate?.setZTCompleteTime(data?.completeTime)
                viewDelegate?.setZTStatus(data?.state)


                when (data?.sysState) {
                    0 -> {
                        //正常
                        if (UserInfoManageUtil.getUserInfo().userId == data.createUser) {
                            if ("0" == data.applyType) {
                                viewDelegate?.setYanQiState(true)
                            }
                            viewDelegate?.setCancelApply(true)
                        } else {
                            viewDelegate?.setYanQiState(false)
                            viewDelegate?.setCancelApply(false)
                        }
                    }
                    1 -> {
                        //延期
                        if (UserInfoManageUtil.getUserInfo().userId == data.createUser) {
                            if ("0" == data.applyType) {
                                viewDelegate?.setYanQiState(true)
                            }

                            viewDelegate?.setCancelApply(true)
                        } else {
                            viewDelegate?.setYanQiState(false)
                            viewDelegate?.setCancelApply(false)
                        }
                    }
                    2 -> {
                        if (UserInfoManageUtil.getUserInfo().userId == data.createUser) {
                            // 预警
                            if ("0" == data.applyType) {
                                viewDelegate?.setYanQiState(true)
                            }
                            viewDelegate?.setCancelApply(true)
                        } else {
                            viewDelegate?.setYanQiState(false)
                            viewDelegate?.setCancelApply(false)
                        }
                    }
                    3 -> {
                        //取消
                        viewDelegate?.setYanQiState(false)
                        viewDelegate?.setCancelApply(false)
                    }
                    4 -> {
                        //完结
                        viewDelegate?.setYanQiState(false)
                        viewDelegate?.setCancelApply(false)
                    }
                }
            }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 101) {
            PartyManagerServer.projectDetail(projectId, true)
        }
    }

}